package com.bili.bili_data_api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bili.bili_data_api.dao.mapper.GaoxiaoAreaMapper;
import com.bili.bili_data_api.dao.pojo.BilibiliWeekly;
import com.bili.bili_data_api.dao.pojo.GaoxiaoArea;
import com.bili.bili_data_api.service.GaoxiaoAreaService;
import com.bili.bili_data_api.util.Result;
import com.bili.bili_data_api.vo.BilibiliWeeklyVo;
import com.bili.bili_data_api.vo.GaoxiaoAreaVo;
import com.bili.bili_data_api.vo.QueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class GaoxiaoAreaServiceImpl implements GaoxiaoAreaService {
    @Autowired
    GaoxiaoAreaMapper gaoxiaoAreaMapper;

    @Override
    public Result edit(GaoxiaoArea gaoxiaoArea) {
        gaoxiaoAreaMapper.updateById(gaoxiaoArea);
        return Result.ok();
    }

    @Override
    public Result delete(GaoxiaoArea gaoxiaoArea) {
        gaoxiaoAreaMapper.deleteById(gaoxiaoArea.getId());
        return Result.ok();
    }

    @Override
    public Result add(GaoxiaoArea gaoxiaoArea) {
        gaoxiaoAreaMapper.insert(gaoxiaoArea);
        return Result.ok();
    }

    @Override
    public GaoxiaoAreaVo selectByTitle(QueryVo queryVo) {
        Page<GaoxiaoArea> page=new Page<>(queryVo.getPageIndex(),queryVo.getPageSize());
        LambdaQueryWrapper<GaoxiaoArea> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(GaoxiaoArea::getTitle,queryVo.getTitle());
        Page<GaoxiaoArea> gaoxiaoAreaPage = gaoxiaoAreaMapper.selectPage(page, lambdaQueryWrapper);
        Integer integer = gaoxiaoAreaMapper.selectCount(lambdaQueryWrapper);
        GaoxiaoAreaVo gaoxiaoAreaVo = new GaoxiaoAreaVo();
        List<GaoxiaoArea> gaoxiaoAreaLists = new ArrayList<>();
        for (GaoxiaoArea gaoxiaoArea : gaoxiaoAreaPage.getRecords()) {
            gaoxiaoAreaLists.add(gaoxiaoArea);
        }
        gaoxiaoAreaVo.setList(gaoxiaoAreaLists);
        gaoxiaoAreaVo.setTotal(integer);
        return gaoxiaoAreaVo;
    }
}
